package com.mobilesales.android.c2dm.parsers;

import java.util.List;

import android.content.Context;
import android.util.Log;

import com.mobilesales.android.database.TaskManager;
import com.mobilesales.android.model.tasks.CyclicTask;
import com.mobilesales.android.model.tasks.Schedule;
import com.mobilesales.android.model.tasks.Task;
import com.mobilesales.android.model.tasks.TaskStatus;
import com.mobilesales.android.model.tasks.TaskType;
import com.mobilesales.android.services.TaskServiceClient;
import com.mobilesales.android.util.Settings;

public class TaskMessageParser extends MessageParserBase {

	private TaskManager manager;
	private TaskServiceClient service;

	public TaskMessageParser(Context context) {
		super(context);
	}
	
	public TaskMessageParser() {
		super();
	}		

	@Override
	public boolean parseMessage(String method, String arg) {
		manager = new TaskManager(getContext());
		service = new TaskServiceClient(Settings.TaskService());
		try {
			this.invokeMethod(TaskMessageParser.class, method,arg);
			return true;
		}
		catch(Exception ex) {
			Log.e("MessageParser", ex.getMessage(), ex);
			return false;
		}
		finally {
			manager.Close();
		}		
	}
	
	public void GetCyclicTask(Integer id) throws Exception {
		CyclicTask task = service.getCyclicTask(id);
		if (task!=null) {
			Log.i("employeeId", task.EmployeeId+"");
			
			if (!manager.StoreCyclicTask(task))
				throw new Exception("Could not store cyclic task");
		}
		else {
			throw new Exception("Retrieved cyclic task is null");
		}			
	}
	
	public void GetTask(Integer id) throws Exception {
		Task task = service.getTask(id);
		if (task!=null) {
			if (!manager.StoreTask(task))
				throw new Exception("Could not store task");
		}
		else {
			throw new Exception("Retrieved task is null");
		}			
	}	
	
	public void DeleteTask(Integer id) throws Exception {
		if (!manager.RemoveTask(id))
			throw new Exception("Could not delete task");
	}
	
	public void GetSchedule(Integer id) throws Exception {

		Schedule schedule = service.getSchedule(id);
		if (schedule!=null) {
			if (!manager.StoreSchedule(schedule))
				throw new Exception("Could not store schedule");

			List<Task> tasks = service.getTaskList(id);
			if (tasks!=null) {
				for(Task t : tasks) {
					if (!manager.StoreTask(t))
						throw new Exception("Could not store schedule task");
				}
			}
			else
				throw new Exception("Could not get schedule tasks");
		}
		else {
			throw new Exception("Retrieved schedule is null");
		}		
	}
	
	public void DeleteSchedule(Integer id) throws Exception {
		if (!manager.RemoveSchedule(id))
			throw new Exception("Could not delete schedule");		
	}

	public void GetTaskType(Integer id) throws Exception {
		TaskType type = service.getTaskType(id);
		if (type!=null) {
			if (!manager.StoreTaskType(type))
				throw new Exception("Could not store task type");
		}
		else {
			throw new Exception("Retrieved task type is null");
		}		
	}	
	
	public void DeleteTaskType(Integer id) throws Exception {
		if (!manager.RemoveTaskTypes(id))
			throw new Exception("Could not delete task status");
	}	

	public void GetTaskStatus(Integer id) throws Exception {
		TaskStatus status = service.getTaskStatus(id);
		if (status!=null) {
			if (!manager.StoreTaskStatus(status))
				throw new Exception("Could not store task status");
		}
		else {
			throw new Exception("Retrieved task status is null");
		}			
	}	
	
	public void DeleteTaskStatus(Integer id) throws Exception {
		if (!manager.RemoveTaskStatus(id))
			throw new Exception("Could not delete task status");
		
	}
}
